package com.itheima.test4;

import java.util.concurrent.*;

/**
 * @Author:gy
 * @Date: 2025/09/03 10:58
 * @Description:
 */
public class Demo {
    public static void main(String[] args) {
        /*
          最大线程数是5,核心线数2,临时线程数是3,一个临时线程空闲2秒,我们就给他销毁
          任务过多,我们就让多的任务在ArrayBlockingQueue里面排队,队列里面只能有10个任务
          线程是从defaultThreadFactory工厂里面创建,如果任务过多,我们就要决绝AbortPolicy.
        * */
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2,
               5,2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 1; i <= 16; i++) {
            pool.submit(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"你好");
            });
        }
        pool.shutdown();
    }
}
